package com.tensquare.user.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Handler;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.tensquare.user.domain.User;
import com.tensquare.user.service.IUserService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户Controller
 * 
 * @author hbxfwf
 * @date 2021-08-25
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;

    /**
     * 功能: 获取验证码
     * 参数:
     * 返回值:
     * 时间: 2021/9/8 15:46
     */
    @GetMapping("/getCode")
    public AjaxResult getCode(String phone){
        userService.getCode(phone);
        return AjaxResult.success();
    }
    /**
     * 功能: 获取用户名
     * 参数:
     * 返回值:
     * 时间: 2021/9/3 14:59
     */
    @GetMapping("/getUsername")
    public AjaxResult getUserName(HttpSession session){
        //经过网络传送后，得到的是一个JSONObject对象,放入的对象是什么无关紧要！
        JSONObject user  = (JSONObject) session.getAttribute("user");
        System.out.println("getUsername = " + user);
        Map map = new HashMap<>();
        if(user != null){
            map.put("name",user.get("nickname"));
            return AjaxResult.success(map);
        }
        return AjaxResult.error("不能取得用户名！");
    }
    /**
     * 查询用户列表
     */
    @PreAuthorize(hasPermi = "user:user:list")
    @GetMapping("/list")

    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @PreAuthorize(hasPermi = "user:user:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user) throws IOException
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize(hasPermi = "user:user:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(userService.selectUserById(id));
    }

    /**
     * 新增用户
     */
    @PreAuthorize(hasPermi = "user:user:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        return toAjax(userService.insertUser(user));
    }

    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody User user)
    {
        return toAjax(userService.insertUser(user));
    }

    @PutMapping("/update")
    public AjaxResult update(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    //修改uid查询是否有此用户
    @GetMapping("/findByUid/{uid}")
    public  User findByUid(@PathVariable("uid") String uid){
        return userService.findByUid(uid);
    }

    /**
     * 修改用户
     */
    @PreAuthorize(hasPermi = "user:user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户
     */
    @PreAuthorize(hasPermi = "user:user:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }
}
